//go:build exclude
// +build exclude

package main

import (
	"log"
	"os"
	"path/filepath"

	"io/fs"

	"entgo.io/ent/entc"
	"entgo.io/ent/entc/gen"
	"github.com/hedwigz/entviz"
)

func main() {
	log.Println("Running Ent code generator")
	log.Println("Cleanup old files")
	filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
		if d.IsDir() && d.Name() == "schema" {
			return filepath.SkipDir
		}
		if d.Name() == "entc.go" || d.Name() == "generate.go" {
			return nil
		}
		if path == "." {
			return nil
		}
		er := os.RemoveAll(path)
		if er != nil {
			log.Printf("cannot remove %v %v", path, er)
		} else {
			log.Println("removed " + path)
		}
		return nil
	})
	if err := entc.Generate("./schema", &gen.Config{
		Features: []gen.Feature{
			gen.FeaturePrivacy,
			gen.FeatureUpsert,
		},
	}, entc.Extensions(entviz.Extension{})); err != nil {
		log.Fatal("running ent codegen:", err)
	}
}
